package servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import javabean.Admin;
import javabean.Base;
import javabean.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebServlet("/admin/orderList")
public class OrderList extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json; charset=utf8");
		// 接受参数
		String limit = req.getParameter("limit");
		String page = req.getParameter("page");
		String condition = (String) req.getParameter("condition");
		String conditionValue = (String) req.getParameter("conditionValue");
		String where = ""; // 无限制条件
		if (page == null) {
			page = "1";
		}
		if (limit == null) {
			limit = "10";
		}
		// 准备参数
		String area = null;
		HttpSession session = req.getSession();
		String adminid = session.getAttribute("admin").toString();
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet resultSet = null;
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		// 返回参数
		int code = 1;
		String msg = "error";
		int count = 0;
		int counts = 0;
		String sql = "";
		PrintWriter out = resp.getWriter();
		// 开始查询
		Admin admin = new Admin();
		try {
			area = admin.area(adminid);
			connection = Base.getConnection();
			sql = "select * from ("+area+"order natural join oorder) left outer join postman on "+area+"order.Pid=postman.Pid where state ='no-propelled' or state ='no-transported'";
			sql += " limit ?,?";// 1 10 (1-1)*10
			System.out.println("???" + sql);
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit));
			pstmt.setInt(2, Integer.parseInt(limit));



			resultSet = pstmt.executeQuery();
			while (resultSet.next()) {
				jsonObject.put("id", resultSet.getString("Oid"));
				if( resultSet.getString("Pname")==null){
					jsonObject.put("name", "未有快递员接单");
				}else {
					jsonObject.put("name", resultSet.getString("Pname"));
				}

				String state = resultSet.getString("state");
				String status = null;
				if(state.equals("no-propelled")){
					status="已抵达中转站等待出站";
				} else if (state.equals("no-transported")) {
					status="待接单";
				}
				jsonObject.put("state",status );
				jsonObject.put("nextstep",resultSet.getString("nextstep") );
				jsonArray.add(jsonObject.toString());
			}
			String sql1 = "select count(*) as count  from "+area+"order ";

			pstmt = connection.prepareStatement(sql1);
			resultSet = pstmt.executeQuery();
			if(resultSet.next()) {
				counts = resultSet.getInt("count");
			}

			if (!jsonArray.isEmpty()) {
				code = 0;
				msg = "查询成功";
			} else {
				msg = "数据为空";
			}
		} catch (ClassNotFoundException e) {
			msg = "没找到";
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
			msg = "sql错误";
		} finally {
			try {
				Base.closeResource(connection, pstmt, resultSet);
			} catch (SQLException e) {
				msg = "关闭失败";
			}
		}
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("code", code);
		jsonResult.put("count", counts);
		jsonResult.put("msg", msg);
		jsonResult.put("data", jsonArray.toArray());

		System.out.println(jsonArray.toString());
		out.print(jsonResult.toString());

	}

}
